﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace MipsPipelineSimulator
{
    public partial class MainForm : Form
    {
        public CommandForm commandForm = null;

        private Image image = null;

        private static object tmp = new object();

        static AutoResetEvent aa = new AutoResetEvent(true);

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
           
            g.DrawImage(image, 0, 0);
        }

        private void KeepStatusToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
        }

        private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void InputCommandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (commandForm == null)
            {
                commandForm = new CommandForm(this);
                commandForm.Height = this.Height;
            }
            //commandForm.SetPosition();
            commandForm.WindowState = System.Windows.Forms.FormWindowState.Normal;
            commandForm.Show();
            commandForm.Activate();
        }

        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DefinedMsgBox msgBox = new DefinedMsgBox("信息", "Mips指令流水线模拟器\n版本Release 1.0!");
            msgBox.ShowDialog(this);
        }

        private void InstructionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.FileName = "notepad.exe";
            psi.Arguments = "help.txt";
            psi.RedirectStandardOutput = true;
            psi.UseShellExecute = false;
            Process p = Process.Start(psi);   
        }

        private void MainForm_Move(object sender, EventArgs e)
        {
            if (commandForm != null && commandForm.Visible)
            {
                if (commandForm.isTouchedWithParent)
                {
                    commandForm.SetPosition();
                }
                else
                {
                    if(commandForm.JudgeIfToTouch())
                    {
                        commandForm.isTouchedWithParent = true;
                        commandForm.SetPosition();
                    }
                }
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MasterSwitch.IsRunning())
            {
                MasterSwitch.Close();
                DefinedMsgBox msgBox = new DefinedMsgBox("提醒", "已设置关闭信号，需要等待该轮绘制结束\n请稍等片刻,再进行关闭!");
                msgBox.ShowDialog(this);
                e.Cancel = true;
                return;
            }
            if (commandForm!=null)
            {
                commandForm.Close();
            }
            if (image != null)
            {
                image.Dispose();
            }
        }

        private Image LoadImage(string path)
        {
            Image image = Image.FromFile(path);
            Image tmp = new Bitmap(image,image.Width, MainFormMenu.Height + image.Height);
            Graphics g = Graphics.FromImage(tmp);
            g.DrawImage(image, new Point(0, MainFormMenu.Height));
            image.Dispose();
            return tmp;
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            image = this.LoadImage("PipeLine.png");
            this.ClientSize = new Size(image.Width,MainFormMenu.Height + image.Height);
            if(!CommonTool.InitialPositions("position.xml", MainFormMenu.Height))
            {
                DefinedMsgBox msgBox = new DefinedMsgBox("错误", "初始化位置失败!");
                msgBox.ShowDialog(this);
                this.Close();
            }
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);

            commandForm = new CommandForm(this);
            commandForm.Height = this.Height;
            commandForm.WindowState = System.Windows.Forms.FormWindowState.Normal;
            commandForm.Hide();
        }

        public Graphics[] GetGraphics()
        {
            return (Graphics[])this.Invoke(new CreateGraphDeletgate(CreateGraph)); 
           // return new Graphics[] {this.CreateGraphics(), Graphics.FromImage(image)};
        }

        private delegate Graphics[] CreateGraphDeletgate();
        private Graphics[] CreateGraph()
        {
            return new Graphics[] { this.CreateGraphics(), Graphics.FromImage(image) };
        }

        private void registerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegisterSetting registerSetting = new RegisterSetting(this);
            registerSetting.ShowDialog(this);
        }

        private void memoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddressSetting addressSetting = new AddressSetting(this);
            addressSetting.ShowDialog(this);
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MasterSwitch.Close();
            DefinedMsgBox msgBox = new DefinedMsgBox("提醒", "已设置关闭信号，需要等待该轮绘制结束\n请稍等片刻，谢谢配合!");
            msgBox.ShowDialog(this);
        }   
    }
}
